<?php
/**
 * Created by PhpStorm.
 * User: tiemeng
 * Date: 15/11/19
 * Time: 15:00
 */

class RenRen {
    /**
     * token终端
     */
    const TOKEN_ENDPOINT = 'https://graph.renren.com/oauth/token?';
    /**
     * 授权终端
     */
    const AUTHORIZATION_ENDPOINT = 'https://graph.renren.com/oauth/authorize?';

    const API_HOST = 'https://api.renren.com/';

    /**
     * Set the useragnet.
     */
    const USERAGENT = 'Renn API2.0 SDK PHP v0.1';
    /**
     * Set timeout default.
     */
    const TIMEOUT = 60;
    /**
     * Set connect timeout.
     */
    const CONNECTTIMEOUT = 30;

    /**
     * 应用id
     */
    protected $clientId = '8abb0a0068744b63a9e82376eb1ab09b';
    /**
     * 应用密钥
     */
    protected $clientSecret = 'aea6b8ea28f940dea37e36983c1e005f';

    /**
     * 回调地址
     */
    protected $callBack = 'http://irun.hupu.com/index.php?c=connect&a=rrCallback';

    public function authorizeUrl(){
        $param = array(
            'client_id' => $this->clientId,
            'redirect_uri' => $this->callBack,
            'response_type' => 'code'
        );
        $query = http_build_query($param,null,'&');
        return self::AUTHORIZATION_ENDPOINT.$query;
    }

    public function getToken($code){
        $param = array(
            'grant_type'=>'authorization_code',
            'client_id'=>$this->clientId,
            'client_secret' => $this->clientSecret,
            'redirect_uri'=> $this->callBack,
            'code' => $code
        );
        $param = http_build_query($param);
        $response = $this->http(self::TOKEN_ENDPOINT,'POST',$param);
        $response = json_decode($response,true);
        return $response;
    }

    public function getUserInfo($token,$uid){
        $param = array(
            'access_token' => $token,
            'userId'       => $uid
        );

        $param = http_build_query($param);
        $url = self::API_HOST.'v2/user/get?'.$param;
        $res = $this->http($url,'GET');
        return json_decode($res,true);
    }

    private function http($url, $method, $postfields = null, $headers = array()) {
        $ci = curl_init ();
        /* Curl settings */
        curl_setopt ( $ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
        curl_setopt ( $ci, CURLOPT_USERAGENT, self::USERAGENT );
        curl_setopt ( $ci, CURLOPT_CONNECTTIMEOUT, self::CONNECTTIMEOUT );
        curl_setopt ( $ci, CURLOPT_TIMEOUT, self::TIMEOUT );
        curl_setopt ( $ci, CURLOPT_RETURNTRANSFER, TRUE );
        curl_setopt ( $ci, CURLOPT_ENCODING, "" );
        curl_setopt ( $ci, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $ci, CURLOPT_SSL_VERIFYHOST, 2 );
        curl_setopt ( $ci, CURLOPT_HEADER, FALSE );

        // 方法
        switch ($method) {
            case 'POST' :
                curl_setopt ( $ci, CURLOPT_POST, TRUE );
                if (! empty ( $postfields )) {
                    curl_setopt ( $ci, CURLOPT_POSTFIELDS, $postfields );
                    $this->postdata = $postfields;
                }
                break;
            case 'DELETE' :
                curl_setopt ( $ci, CURLOPT_CUSTOMREQUEST, 'DELETE' );
                if (! empty ( $postfields )) {
                    $url = "{$url}?{$postfields}";
                }
        }

        curl_setopt ( $ci, CURLOPT_URL, $url );
        curl_setopt ( $ci, CURLOPT_HTTPHEADER, $headers );
        curl_setopt ( $ci, CURLINFO_HEADER_OUT, TRUE );

        $response = curl_exec ( $ci );

        curl_close ( $ci );
        return $response;
    }
}